package com.yzz.hzraisedust.hzraseidust.main;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.yzz.hzraisedust.hzraseidust.R;
import com.yzz.hzraisedust.hzraseidust.main.alarm.AlarmAdapter;
import com.yzz.hzraisedust.hzraseidust.main.squaredetail.SquareDetailInfoActivity;
import com.yzz.hzraisedust.hzraseidust.modelbean.AlarmInfoBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.AlarmItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.RequstItemBean;
import com.yzz.hzraisedust.hzraseidust.util.Constant;
import com.yzz.hzraisedust.hzraseidust.util.NetUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link } interface
 * to handle interaction events.
 * Use the {@link AlarmFragment} factory method to
 * create an instance of this fragment.
 */
public class AlarmFragment extends Fragment implements Response.Listener<String>,Response.ErrorListener,AdapterView.OnItemClickListener{
    public static final String TAG = MapInfoFragment.class.getName();
    private static AlarmFragment Instance;
    private ListView mListView;
    private AlarmInfoBean items;
    public AlarmFragment() {
        // Required empty public constructor
    }

    public static AlarmFragment getInstance(){
        if(Instance == null){
            Instance = new AlarmFragment();
        }
        return Instance;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_alarm, container, false);
        mListView = view.findViewById(R.id.alram_info_listView);
        mListView.setEmptyView(view.findViewById(R.id.alram_empty_list_textview));
        mListView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        ((BaseActivity)getActivity()).showLoginingDlg();
        NetUtil netUtil = new NetUtil(getActivity());
        Map<String,String> param = new HashMap<>();
        param.put(Constant.UID_KEY,Constant.Uid);

        netUtil.requestData(this,null, Constant.SQUARE_URL,param);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    @Override
    public void onResponse(String s) {
        Gson gson = new Gson();
        items = gson.fromJson(s,AlarmInfoBean.class);
        if(items == null || !items.isSuccess()){
            if(getActivity()!=null) {
                ((BaseActivity) getActivity()).closeLoginingDlg();
            }
            Toast.makeText(getActivity(),"获取数据失败",Toast.LENGTH_LONG).show();
            return;
        }
        AlarmAdapter adapter = new AlarmAdapter(getActivity(),items.getRows());
        mListView.setAdapter(adapter);
        if(getActivity()!=null) {
            ((BaseActivity) getActivity()).closeLoginingDlg();
        }
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        if(getActivity()!=null) {
            ((BaseActivity) getActivity()).closeLoginingDlg();
        }
        Toast.makeText(getActivity(),"获取数据失败"+volleyError.getLocalizedMessage(),Toast.LENGTH_LONG).show();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        AlarmItemBean itemBean = items.getRows().get(i);
        Intent intent = new Intent(getActivity(), SquareDetailInfoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constant.ALARM_ITEM_BEAN,itemBean);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
